package com.ruoyi.project.system.sub.controller;

import java.util.List;

import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.project.system.role.domain.Role;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.domain.UserRole;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.sub.domain.JobSub;
import com.ruoyi.project.system.sub.service.IJobSubService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 提交作业Controller
 * 
 * @author ruoyi
 * @date 2021-12-26
 */
@Controller
@RequestMapping("/jobsub/sub")
public class JobSubController extends BaseController
{
    private String prefix = "jobsub/sub";

    @Autowired
    private IJobSubService jobSubService;

    @RequiresPermissions("jobsub:sub:view")
    @GetMapping()
    public String sub()
    {
        return prefix + "/sub";
    }

    /**
     * 查询提交作业列表
     */
    @RequiresPermissions("jobsub:sub:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(JobSub jobSub)
    {
        Long userId = ShiroUtils.getUserId();

        User sysUser = ShiroUtils.getSysUser();
        List<Role> roles=sysUser.getRoles();
        Role sysRole=roles.get(0);
       // List<SysRole> roles = sysUser.getRoles();
        //SysRole sysRole = roles.get(0);
        /*//教师
        if(sysRole.getRoleId()==3){
            //如果是老师角色只显示当前老师操作的数据
            JobSub.setTeacherId(userId);//.setStudentId(userId);
        }//学生
        else if(sysRole.getRoleId()==4){
            //如果是学生角色只显示当前学生操作的数据
        JobSub.setTeacherId(String.valueOf(userId));*/


        startPage();
        List<JobSub> list = jobSubService.selectJobSubList(jobSub);
        return getDataTable(list);
    }

    /**
     * 导出提交作业列表
     */
    @RequiresPermissions("jobsub:sub:export")
    @Log(title = "提交作业", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(JobSub jobSub)
    {
        List<JobSub> list = jobSubService.selectJobSubList(jobSub);
        ExcelUtil<JobSub> util = new ExcelUtil<JobSub>(JobSub.class);
        return util.exportExcel(list, "提交作业数据");
    }

    /**
     * 新增提交作业
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存提交作业
     */
    @RequiresPermissions("jobsub:sub:add")
    @Log(title = "提交作业", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(JobSub jobSub)
    {
        return toAjax(jobSubService.insertJobSub(jobSub));
    }

    /**
     * 修改提交作业
     */
    @RequiresPermissions("jobsub:sub:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        JobSub jobSub = jobSubService.selectJobSubById(id);
        mmap.put("jobSub", jobSub);
        return prefix + "/edit";
    }

    /**
     * 修改保存提交作业
     */
    @RequiresPermissions("jobsub:sub:edit")
    @Log(title = "提交作业", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(JobSub jobSub)
    {
        return toAjax(jobSubService.updateJobSub(jobSub));
    }

    /**
     * 删除提交作业
     */
    @RequiresPermissions("jobsub:sub:remove")
    @Log(title = "提交作业", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(jobSubService.deleteJobSubByIds(ids));
    }
}